﻿using System.Windows;
using Microsoft.Phone.Scheduler;

using System.Linq;
using System.Net;
using System;
using System.Data.Linq;
using System.Text.RegularExpressions;

using System.Collections.Generic;
using System.IO.IsolatedStorage;
using OnlineVideos.Library;
using OnlineVideos.Entities;
using Common.Library;



namespace WebSliceBackAgent
{
    public class ScheduledAgent : ScheduledTaskAgent
    {
       
        protected override void OnInvoke(ScheduledTask task)
        {
            try
            {
                PersonalizationDataSync p = new PersonalizationDataSync();
                p.UploadSearch();
                OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
                if (context.Share.FirstOrDefault() != null)
                {
                    foreach (ShareTable st in context.Share.Where(i => i.NextPostTime < System.DateTime.Now).Select(i => i).ToList())
                    {
                        UploadToBlog ub = new UploadToBlog(st.ShowID, st.Description, st.ImageUrl);
                        break;
                    }
                }

                if (context.Dailytable.Where(i => i.Status == "Changed").OrderByDescending(i=>i.Date).FirstOrDefault()!=null)
                {
                    IQueryable<WebDailyTable> DTable = context.Dailytable.Where(i => i.Status == "Changed").OrderByDescending(i => i.Date).Take(5);
                    foreach (WebDailyTable wt in DTable.Where(i=>i!=null))
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            CreateTileImage CI = new CreateTileImage(wt.ShowID.ToString(), this);
                        });
                    }
                }
                else
                {
                    NotifyComplete();
                }
            }
            catch (Exception ex)
            {
                NotifyComplete();
            }

        }
        public void complete()
        {
            NotifyComplete();
        }
    }
}